package data;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;


import models.Artist;
import models.ArtistList;
import models.MajorStyle;
import models.MajorStyleList;
import models.SubStyle;
import models.SubStyleList;


//Reference: http://www.csvreader.com/java_csv_samples.php

public class DataParser {
	public static void initData(){
		try {
			
			
			// ARTIST----------------------------------------------
			CsvReader artistsCSV = new CsvReader("data/Artist.txt");
			ArtistList al= ArtistList.getInstance();
			artistsCSV.readHeaders();
			//# pgid, tier, artist, act_yrs, existence_year, born_form_place, ceased_year, ceased_place, amgid, born_name, is_solo
			while (artistsCSV.readRecord())
			{
				
				try{

					String pgid = artistsCSV.get("# pgid");
					String tier = artistsCSV.get("tier");
					String name = artistsCSV.get("artist");
					String act_yrs = artistsCSV.get("act_yrs");
					String existence_year = artistsCSV.get("existence_year");
					String born_form_place = artistsCSV.get("born_form_place");
					String ceased_year = artistsCSV.get("ceased_year");
					String ceased_place = artistsCSV.get("ceased_place");
					String amgid = artistsCSV.get("amgid");
					String born_name = artistsCSV.get("born_name");
					String is_solo = artistsCSV.get("is_solo");
					
					//add artist
					al.addArtist(new Artist(pgid,tier,name,act_yrs, existence_year,born_form_place, ceased_year,ceased_place,amgid,born_name,is_solo));
					
				}catch (Exception e) {
					System.out.println("Artist data mismatch");
				}
			}
	
			artistsCSV.close();
			
			//ARTIST INFLUENCER------------------------------------
			CsvReader artistInfluencedCSV = new CsvReader("data/Artist_influenced_by.txt");
		
			artistInfluencedCSV.readHeaders();
			//# pgid, influencer_artist, influencer_pgid, influencer_genre
			while (artistInfluencedCSV.readRecord())
			{
				try {
					al.getArtistByPGID(artistInfluencedCSV.get("# pgid")).addArtistInfluencer(al.getArtistByPGID(artistInfluencedCSV.get("influencer_pgid")));
				} catch (Exception e) {
						System.out.println("Artist influencer data mismatch");
				}
					

			}
	
			artistInfluencedCSV.close();
			//STYLE------------------------------------------
			CsvReader styleCSV = new CsvReader("data/Major_market_to_market_key.txt");
			MajorStyleList msl= MajorStyleList.getInstance();
			SubStyleList ssl= SubStyleList.getInstance();
			styleCSV.readHeaders();
			MajorStyle ms;
			//# market, Major_market
			while (styleCSV.readRecord())
			{
				
				try{
					//Create MajorStyle
					//If there is no major sytle already create with this name
					if(msl.getMajorStyleByName(styleCSV.get("Major_market"))==null){
						ms= new MajorStyle(styleCSV.get("Major_market"));
						msl.addMajorStyle(ms);
					}
					else{
						ms= msl.getMajorStyleByName(styleCSV.get("Major_market"));
					}
					
					ssl.addSubStyle(new SubStyle(ms,styleCSV.get("# market")));
				}
				catch (Exception e) {
					System.out.println("Style data mismatch");
				}
				
				
			}
			styleCSV.close();
			
			//APPLY STYLE TO ARTIST----------------------------------------------
			CsvReader artistStyleCSV = new CsvReader("data/Artist_market_or_style.txt");
			artistStyleCSV.readHeaders();
			//# pgid, market
			while (artistStyleCSV.readRecord())
			{
				try {
					al.getArtistByPGID(artistStyleCSV.get("# pgid")).setSubStyle(ssl.getSubStyleByName(artistStyleCSV.get("market")));
				} catch (Exception e) {
						System.out.println("Artist Style data mismatch");
				}
				
				
			}
			artistStyleCSV.close();
			
			//Print all artist
			ArrayList<Artist> artistList= al.getArtistList();
			System.out.println("Artist total:"+ artistList.size());
			for(int i=0;i<artistList.size();i++){
				System.out.println(i+":"+artistList.get(i));
			}
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}


	
}

